
package com.leessy.r229.utils

import android.app.admin.DevicePolicyManager
import android.content.Context
import android.os.Build
import android.provider.Settings
import com.leessy.r229.App
import com.leessy.r229.MainActivity

object DevicesUtil {

    fun getDeviceSN(): String {
        return MainActivity.getSystemProperties("ro.serialno")
    }

    fun getSystemVer(): String {
        return Build.DISPLAY
    }

    fun getEth0Mac(): String {
        return MacAddressUtils.getEth0Mac()
    }

    fun getWlanMac(): String {
        return MacAddressUtils.getWlanMac()
    }

    fun getMemoryTotal(): String {
        return MemoryStatus.formatSize(MemoryStatus.getTotalInternalStorageSize());
    }

    fun getMemoryAvailable(): String {
        return MemoryStatus.formatSize(MemoryStatus.getAvailableInternalMemorySize());
    }

    fun getRamTotal(): String {
        return MemoryStatus.formatSizeRam(MemoryStatus.getTotalRamSize());
    }

    fun getRamUsed(): String {
        return MemoryStatus.formatSizeRam(MemoryStatus.getUsedRamSize());
    }

    fun getDeviceName(): String {
        return Settings.Global.getString(App.app.getContentResolver(), Settings.Global.DEVICE_NAME)
    }

    fun setDeviceName(string: String) {
        Settings.Global.putString(App.app.getContentResolver(), Settings.Global.DEVICE_NAME, string)
    }

    fun setDeviceLog(b: Boolean) {
        setSystemProperties("persist.sys.yf_boot_get_log", if (b) "1" else "0")
//        System.setProperty("persist.sys.yf_boot_get_log", if (b) "1" else "0")
//        Settings.Global.putString(App.app.getContentResolver(), Settings.Global.DEVICE_NAME, string)
    }

    fun getDeviceIccid(): String {
        return "" + getSystemProperties("ril.yuga.iccid")
    }

    //测试锁屏唤醒 锁屏
    fun lockAcquire() {
        var devicePolicyManager =
            App.app.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        devicePolicyManager.lockNow()
//        Thread {
//            try {
//                Thread.sleep(5000)
//            } catch (e: InterruptedException) {
//            }
//            // 唤醒屏幕
//            val powerManager = App.app.getSystemService(Context.POWER_SERVICE) as PowerManager
//            val wakeLock = powerManager.newWakeLock(
//                PowerManager.FULL_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
//                "MyApp:WakeLock"
//            )
//            wakeLock.acquire()
//        }.start()
    }

    fun getSystemProperties(s: String?): String? {
        var serial: String? = null
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("get", String::class.java)
            serial = get.invoke(c, s) as String
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return serial
    }

    private fun setSystemProperties(s: String, v: String) {
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("set", String::class.java, String::class.java)
            get.invoke(c, s, v)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

}


